package com.bjpowernode.singleton.attack;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @ClassName ReflectionSingleton08
 *
 * @Author Cat
 */
public enum ReflectionSingleton08 {
    INSTANCE;

    public String sayHi() {
        //java.lang.Enum
        return "hahahahahahaha..........";
    }

    public static void main(String[] args) throws Exception {
        //System.out.println(Integer.toBinaryString(16401)); //100000000010001
        //System.out.println(Integer.toBinaryString(16384)); //100000000000000

        //ReflectionSingleton07 instance = ReflectionSingleton07.getInstance();
        //System.out.println(instance);

        /*Constructor constructor = ReflectionSingleton08.class.getDeclaredConstructor(String.class, int.class);
        constructor.setAccessible(true);//对私有构造器开放权限
        ReflectionSingleton08 instance2 = (ReflectionSingleton08) constructor.newInstance();
        System.out.println(instance2);*/

        //System.out.println(instance1 == instance2);

        //序列化
        ReflectionSingleton08 instance1 = ReflectionSingleton08.INSTANCE;
        System.out.println(instance1);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("SerSingleton.obj"));
        oos.writeObject(instance1);
        oos.flush();
        oos.close();

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("SerSingleton.obj"));
        ReflectionSingleton08 rs01 = (ReflectionSingleton08)ois.readObject();
        ois.close();
        System.out.println(rs01);
        System.out.println(instance1 == rs01);
    }
}